package com.yanqu.road.server.manager.tomb.dao;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.dbV2.serializer.Helper;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.server.manager.tomb.entity.DbTombUserPosition;

import java.sql.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class TombUserPositionDaoImpl extends TempDataObjectDao<DbTombUserPosition> {


    public static TombUserPositionDaoImpl newInstance() {
        TombUserPositionDaoImpl dao = new TombUserPositionDaoImpl();
        return dao;
    }

    public static String updateSql = "update `t_c_activity_tomb_user_position` set eventId=?,relationIdList=?,relationId=?,updateTime=?,groupId=? where activityId=? and userId=? and floor=? and x=? and y=?";

    @Override
    protected boolean updateData(DbTombUserPosition data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, Helper.toJSONString(data.getRelationIdList())));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getRelationId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));

        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getFloor()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getX()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getY()));

        if (getDb().executeNoneQuery(updateSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSql = "insert into `t_c_activity_tomb_user_position`(activityId,userId,floor,x,y,eventId,relationIdList,relationId,createTime,updateTime,groupId) values (?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DbTombUserPosition data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getFloor()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getX()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getY()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, Helper.toJSONString(data.getRelationIdList())));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getRelationId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }


    @Override
    protected DbTombUserPosition parseData(ResultSet rs) throws SQLException {
        DbTombUserPosition data = DbTombUserPosition.newInstance();
        data.setId(rs.getLong("id"));
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setFloor(rs.getInt("floor"));
        data.setX(rs.getInt("x"));
        data.setY(rs.getInt("y"));
        data.setEventId(rs.getInt("eventId"));
        data.setRelationIdList(Helper.toObject(rs.getString("relationIdList"), new TypeReference<HashSet<Integer>>() {
        }));
        if (data.getRelationIdList() == null) {
            data.setRelationIdList(new HashSet<>());
        }
        data.setRelationId(rs.getLong("relationId"));

        data.setCreateTime(rs.getLong("createTime"));
        data.setUpdateTime(rs.getLong("updateTime"));
        data.setGroupId(rs.getInt("groupId"));
        return data;
    }


    public Map<Long, DbTombUserPosition> getDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, DbTombUserPosition> dataMap = new ConcurrentHashMap<>();
        String selectSql = "select * from t_c_activity_tomb_user_position where activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DbTombUserPosition data = parse(rs);
                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public boolean saveData(DbTombUserPosition data) {
        if (data.isUpdateOption()) {
            if (data.isNeedInsert()) {
                if (addData(data)) {
                    data.setNeedInsert(false);
                }
            } else {
                data.setUpdateTime(System.currentTimeMillis());
                update(data);
            }
        }
        return true;
    }

}
